﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using CP.Controllers.GPM;
using CP.Entities;
using CP.Utility; 
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace GPM
{
    public partial class FrmUserGroup : BaseForm
    {
        public GridCheckMarksSelection selection;

        public FrmUserGroup()
        {
            InitializeComponent();
            this.BarComponents = components;
        }

        private void FrmUserGroup_Load(object sender, EventArgs e)
        {
            selection = new GridCheckMarksSelection(this.gvUserGroup);
            RefreshUserGroup();
        }

        public void RefreshUserGroup()
        {
            UserGroupController uc = new UserGroupController();
            IList<User_Group> groups = uc.GetAllUserGroups();
            this.lookupeditParentID.DataSource = groups;
            //selection.ClearSelection();
            this.gcUserGroup.DataSource = groups;
        }

        private void barQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshUserGroup();
        }

        private void barDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (0 == selection.SelectedCount)
            {
                ExMessageBox.Show("提示信息：请选中要删除的记录！", AppSetter.SoftName);
                return;
            }

            if (ExMessageBox.Confirm("提示信息：确定要删除选中的记录吗？", AppSetter.SoftName) == DialogResult.No)
                return;
             
                IList<int> groupids = new List<int>();
                foreach (object o in selection.Selection)
                {
                    User_Group group = o as User_Group;
                    if (null != group)
                        groupids.Add(group.group_id);
                }
                try
                {
                    UserGroupController gc = new UserGroupController();
                    gc.DeleteUserGroups(groupids);
                    LogHelper.Log("角色管理", "delete","删除", "groupids:" + string.Join(",", groupids));
                    ExMessageBox.Show("提示信息：删除成功！", AppSetter.SoftName);
                    RefreshUserGroup();
                }
                catch (Exception ex)
                {
                    ExMessageBox.Show(ex.Message, AppSetter.SoftName);
                }
            
        }

        private void barAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FrmEditUserGroup frmedit = new FrmEditUserGroup();
            frmedit.ShowDialog(this);
        }

        private void barEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (selection.SelectedCount != 1)
            {
                ExMessageBox.Show("提示信息：请选中一条记录编辑", AppSetter.SoftName);
                return;
            }
            User_Group info = selection.Selection[0] as User_Group;
            if (null == info)
            {
                ExMessageBox.Show("提示信息：选中的记录不存在", AppSetter.SoftName);
                return;
            }
            FrmEditUserGroup frmedit = new FrmEditUserGroup("1", info.group_id);
            frmedit.ShowDialog(this);
        }

        private void barSetUser_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (selection.SelectedCount != 1)
            {
                ExMessageBox.Show("提示信息：请选中一条用户记录，设定用户", AppSetter.SoftName);
                return;
            }

            User_Group info = selection.Selection[0] as User_Group;
            if (null == info)
            {
                ExMessageBox.Show("提示信息：选中的记录不存在", AppSetter.SoftName);
                return;
            }

            FrmSetRoleUser frm = new FrmSetRoleUser(info);
            frm.ShowDialog(this);
        }

        private void gvUserGroup_DoubleClick(object sender, EventArgs e)
        {
            User_Group group = gvUserGroup.GetFocusedRow() as User_Group;
            if (null == group)
                return;

            FrmSetRoleUser frm = new FrmSetRoleUser(group);
            frm.ShowDialog(this);
        }
    }
}
